#pragma once 

#include <iostream>
#include "TcpServer.hpp"
#include "Protocol.hpp"

#define PORT 8081

class HttpServer
{
private:
    int port;
    TcpServer* tcp_svr;
    bool stop;

public:
    HttpServer()
    :port(PORT)
    ,tcp_svr(nullptr)
    ,stop(false)
    {
    }

    void InitServer()
    {
        tcp_svr = TcpServer::GetInstance(port);
    }

    void Loop()
    {
        int listen_sock = tcp_svr->Sock();
        LOG(INFO,"Loop start!");
        while(!stop)
        {
            struct sockaddr_in peer;
            socklen_t len = sizeof(peer);
            int sock = accept(listen_sock,(struct sockaddr*)&peer,&len);
            if(sock < 0)
            {
                continue;
            }
            LOG(INFO,"Get a new line");
            int* _sock = new int(sock);
            pthread_t tid;
            pthread_create(&tid,nullptr,Entrance::HandlerRequest,(void*)_sock);
            pthread_detach(tid);
        }
    }

    ~HttpServer()
    {}

};